package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseFragment
import com.lancoo.znbkxx.teacher.model.data.TopicZsd
import com.lancoo.znbkxx.teacher.net.BkApiMethods
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.fragment_unstudy_vocab.*
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeoutException

/**
 *@author Admin
 *@date 2021/1/4
 *
 */
class UnStudyTopicListFragment : BaseFragment() {
    private var totalSize = 0
    private var page = 1
    private val zsd by lazy { arguments?.getString("zsd") }
    private val type by lazy { arguments?.getInt("type") }
    private val stuID by lazy { arguments?.getString("stuID") }
    private val topicList = mutableListOf<TopicZsd>()
    private lateinit var emptyView:View
    private lateinit var adapter: UnStudyTopicListAdapter
    override fun createLayout(): Int {
        return R.layout.fragment_unstudy_vocab
    }

    override fun initView(view: View?) {
        adapter = UnStudyTopicListAdapter(type!!, topicList)
        emptyView=layoutInflater.inflate(R.layout.view_empty_dignosis,null)
        adapter.emptyView=emptyView
        rv_vocab.layoutManager = LinearLayoutManager(requireContext())
        rv_vocab.adapter = adapter
        swipe_layout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                topicList.clear()
                getStuUnStudyVocabInfoList()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (topicList.size <= totalSize) {
                    page += 1
                }
                getStuUnStudyVocabInfoList()
            }
        })
        getStuUnStudyVocabInfoList()
    }

    @SuppressLint("CheckResult")
    fun getStuUnStudyVocabInfoList() {
        val method = BkApiMethods(requireContext())
        val params = BkApiMethods.Paramas()
        params["ZsdArea"] = zsd
        params["VocabType"] = type
        params["Page"] = page
        params["StuID"]=stuID
        method.getStuUnStudyTopicZsdInfoList(Ext.RZZD_URL, params).subscribe({
            if (it.Code == 1) {
                tv_total.setTextColor(Color.parseColor(when (type) {
                    0 -> "#00afff"
                    1 -> "#22d7bb"
                    else -> "#999999"
                }))
                tv_total.text = when (type) {
                    0 -> "常考语法(${it.Data.FirstTopicZsd}个)"
                    1 -> "次常考语法(${it.Data.SecondTopicZsd}个)"
                    else -> "不常考语法(${it.Data.ThirdTopicZsd}个)"
                }
                totalSize = when (type) {
                    0 -> it.Data.FirstTopicZsd
                    1 -> it.Data.SecondTopicZsd
                    else -> it.Data.ThirdTopicZsd
                }
                topicList.addAll(when (type) {
                    0 -> it.Data.FirstTopicZsdList
                    1 -> it.Data.SecondTopicZsdList
                    else -> it.Data.ThirdTopicZsdList
                })
                adapter.setNewData(topicList)
                swipe_layout.finishRefresh()
                swipe_layout.setEnableLoadMore(topicList.size < totalSize)
                if (topicList.size >= totalSize) {
                    swipe_layout.finishLoadMoreWithNoMoreData()
                } else {
                    swipe_layout.finishLoadMore()
                }
            } else {
                MyToast.showToast(it.Message)
            }
        }, {
            val error: String = if (it is TimeoutException || it is SocketTimeoutException || it is ConnectException) {
                getString(R.string.aibk_network_timeout)
            } else {
                getString(R.string.aibk_server_sick)
            }
            MyToast.showToast(error)
        })
    }
}